//<template>
    <div>
        <div>
            <tr>
                <td>产品名</td>
                <td>产品价格</td>
                <td>库存</td>
                <td>购买</td>
            </tr>
            <tr v-for="i in order_list" :key="i.id">
                <td>{{i.good}}</td>
                <td>{{i.price}}</td>
                <td>{{i.inventory}}</td>
                <td><button @click="toEstimate(i.id)">购买</button></td>
            </tr>
        </div>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data(){
        return{
            order_list:[],
            id:sessionStorage.getItem('user_id'),
            url:'http://127.0.0.1:8000',
            num:1
        }
    },
    methods:{
        orderShow(){
            axios({
                url:'http://127.0.0.1:8000/workapp/product_show/'
            }).then(res=>{
                this.order_list = res.data
            })
        },
        toEstimate(id){
            if(this.id){
                let form_data = new FormData()
                form_data.append('product',id)
                form_data.append('user',this.id)
                form_data.append('num',this.num)
                axios({
                    url:'http://127.0.0.1:8000/workapp/cart_add/',
                    method:'post',
                    data:form_data
                }).then(res=>{
                    console.log(res.data)
                })
            }
            else{
                this.$router.push({path:'/loginc/'})
            }
        }
    },
    created(){
        this.orderShow()
    }
}
</script>

<style>

</style>